/***********************************************************************************************************************
**	filename: cfg_info.c
**	author: Echo Zhang @2021-05-23
**  e-mail: not_xx@126.com
***********************************************************************************************************************/
#include "dbg.h"
#include "cfg_info.h"
#include "cJSON.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static bool _process_apn_info_conf(cJSON *jsonData, struct apn_info *_config)
{
    int num = cJSON_GetArraySize(jsonData);

    if(num < 3) {
        return false;
    }
    strcpy(_config->apn, cJSON_GetArrayItem(jsonData, 0)->valuestring);
    strcpy(_config->user, cJSON_GetArrayItem(jsonData, 1)->valuestring);
    strcpy(_config->passwd, cJSON_GetArrayItem(jsonData, 2)->valuestring);

    return true;
}

#if 0
static cJSON *_save_sql_info_conf(struct sql_info *_config)
{
    cJSON *arrayJson = cJSON_CreateArray();
    cJSON_AddItemToArray(arrayJson, cJSON_CreateString(_config->addr));
    cJSON_AddItemToArray(arrayJson, cJSON_CreateNumber(_config->port));
    cJSON_AddItemToArray(arrayJson, cJSON_CreateString(_config->user));
    cJSON_AddItemToArray(arrayJson, cJSON_CreateString(_config->passwd));
    cJSON_AddItemToArray(arrayJson, cJSON_CreateString(_config->db));
    return arrayJson;
}
#endif

static bool _process_tcp_info_conf(cJSON *jsonData, struct server_info *_config)
{
    int num = cJSON_GetArraySize(jsonData);

    if(num < 3) {
        return false;
    }

    strcpy(_config->addr, cJSON_GetArrayItem(jsonData, 0)->valuestring);
    _config->port = cJSON_GetArrayItem(jsonData, 1)->valueint;
    strcpy(_config->file_path, cJSON_GetArrayItem(jsonData, 2)->valuestring);

    return true;
}

bool cfg_info_load(const char *filename, struct cfg_info *cfgInfo)
{
    FILE *fp = fopen(filename, "r");
    if(fp == NULL) {
        return false;
    }
    char buffer[1024*100];
    int len = fread(buffer, 1, sizeof(buffer)-1, fp);
    fclose(fp);
    if(len < 0) {
        return false;
    }
    buffer[len] = 0;
    cJSON *root = cJSON_Parse(buffer);
    if(root == NULL) {        
        return false;
    }

    cJSON *apnJson = cJSON_GetObjectItem(root, "apn_info");
    if (apnJson != NULL) {
        _process_apn_info_conf(apnJson, cfgInfo->_apn);
    } 
    
    cJSON *tcpJson = cJSON_GetObjectItem(root, "server_info");
    if (tcpJson != NULL) {
        _process_tcp_info_conf(tcpJson, cfgInfo->_tcp);
    } 

    return true;
}

#if 0
bool cfg_info_save(const char *filename, const struct cfg_info *cfgInfo)
{
    cJSON *root = cJSON_CreateObject();
    cJSON_AddItemToObject(root, "sql_info", _save_mqtt_info_conf(cfgInfo->_sql));

    FILE *fp = fopen(filename, "w");
    if(fp != NULL) {
        char *string = cJSON_Print(root);
        fprintf(fp, "%s", string);
        free(string);
        fclose(fp);
        dbg_msg("配置文件保存完成！");
    } else {
        dbg_err("配置文件[%s]保存失败！~", filename);
    }
    free(root);
#ifdef __arm__
    system("sync");
#endif
    return fp!=NULL;
}
#endif

void print_config(const struct cfg_info *cfgInfo)
{
    printf("\n");
    printf("============= Load Config ===============\n");
    printf("******** APN info ********\n");
    printf("apn: %s\n", cfgInfo->_apn->apn);
    printf("user: %s\n", cfgInfo->_apn->user);
    printf("passwd: %s\n", cfgInfo->_apn->passwd);
    printf("******** Server info ********\n");
    printf("address: %s\n", cfgInfo->_tcp->addr);
    printf("port: %d\n", cfgInfo->_tcp->port);
    printf("path: %s\n", cfgInfo->_tcp->file_path);
    printf("========================================\n");   
}


/***********************************************************************************************************************
**	endline
***********************************************************************************************************************/
